package cn.com.nes.site.controller.crm.customer;

import cn.com.nes.mybatis.agent.crm.entity.CustomerStaff;
import cn.com.nes.site.controller.crm.customer.vo.CustomerStaffDeleteVO;
import cn.com.nes.site.controller.crm.customer.vo.CustomerStaffPageVO;
import cn.com.nes.site.controller.crm.customer.vo.CustomerStaffSaveVO;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.service.crm.customer.CustomerStaffService;
import cn.com.nes.site.service.crm.customer.dto.CustomerStaffDTO;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Api(tags = {"销售管理-客户管理-联系人管理"})
@Controller
@RequestMapping("/v2/crm/customer/staff")
public class CustomerStaffController {

    @Resource
    private CustomerStaffService customerStaffService;

    @ApiOperation(value = "查询客户联系人分页")
    @RequestMapping(value = "/getStaffPage", method = RequestMethod.GET)
    public @ResponseBody PageData getStaffPage(@Validated CustomerStaffPageVO customerStaffPageVO) throws Exception {
        Page<CustomerStaffDTO> customerStaffPage = customerStaffService.getCustomerStaffPage(customerStaffPageVO.convertToBo());
        return PageData.ok(customerStaffPage, customerStaffPage.getRecords());
    }


    @ApiOperation(value = "保存客户联系人")
    @RequestMapping(value = "/saveCustomerStaff", method = RequestMethod.POST)
    public @ResponseBody PageData saveCustomerStaff(@RequestBody @Validated CustomerStaffSaveVO customerStaffSaveVO) throws Exception {
        CustomerStaff customerStaff = customerStaffService.saveCustomerStaff(customerStaffSaveVO.convertToBo());
        if(ObjectUtil.isNotNull(customerStaff)) {
            return PageData.ok("staffId", customerStaff.getId());
        }
        return PageData.error("户主只能添加一个!");
    }

    @ApiOperation(value = "删除客户联系人")
    @RequestMapping(value = "/deleteCustomerStaff", method = RequestMethod.POST)
    public @ResponseBody PageData deleteCustomerStaff(@RequestBody @Validated CustomerStaffDeleteVO customerStaffDeleteVO) throws Exception {
        customerStaffService.deleteCustomerStaff(customerStaffDeleteVO.convertToBo());
        return PageData.ok();
    }


}
